/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import BUSServices.NhaXuatBan;
import com.mysql.jdbc.Connection;
import java.awt.Dialog;
import java.awt.Frame;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Admin
 */
public class TimSach extends javax.swing.JDialog {

    /**
     * Creates new form TimSach
     */
    public TimSach() {
        initComponents();
    }
    
    
    public TimSach(Frame owner) {
        super(owner);
        initComponents();
    }

    public TimSach(Dialog owner) {
        super(owner);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSach = new javax.swing.JTable();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jcbTheLoai = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jtbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbDanhSach);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Mã Sách:");

        jButton1.setText("Tìm");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Tìm Nâng Cao");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jcbTheLoai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTheLoaiActionPerformed(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Thể Loại:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jcbTheLoai, 0, 177, Short.MAX_VALUE))
                        .addGap(62, 62, 62)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jcbTheLoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        this.TimKiem();
    }//GEN-LAST:event_jButton1ActionPerformed
    public void TimKiem()
    {
        try
        {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            //tao ket noi toi csdl
            String conectionString = "jdbc:mysql://localhost:3306/bookonlinev3";
            Properties pros = new Properties();
            pros.setProperty("characterEncoding", "utf8");
            //pros.setProperty("root","root");
            Connection connect = (Connection) DriverManager.getConnection(conectionString, "root", "root");
             //tao statement
            Statement sm = connect.createStatement();
            Vector vt = (Vector)  this.jcbTheLoai.getSelectedItem();
            int MaLoaiSach = (Integer) vt.get(0);            
            String maSach = this.jTextField1.getText();           
            if(!maSach.equals("")){
            String sql = "SELECT *" + "FROM sach " + " WHERE ma_sach = " + maSach + " AND ma_loai_sach = " + MaLoaiSach;            
            ResultSet rs = sm.executeQuery(sql);
            
            //Tao cac cot tren tabel
            DefaultTableModel dtm = new DefaultTableModel();
            
            dtm.addColumn("Mã Sách");
            dtm.addColumn("Tên Sách");
            dtm.addColumn("Tác Giả");
            dtm.addColumn("Số Lượng Tồn");
            dtm.addColumn("Ngày Nhập");
            dtm.addColumn("Giá Bán");
            
            //doc du lieu
             while(rs.next())
            { 
                Vector ct = new Vector();
                ct.add(rs.getString("ma_sach"));
                ct.add(rs.getString("ten_sach"));
                ct.add(rs.getString("tac_gia"));
                ct.add(rs.getString("so_luong_ton"));
                ct.add(rs.getString("ngay_nhap"));
                ct.add(rs.getString("gia_ban"));
                dtm.addRow(ct);                
            }
            
            //gan model cho jtable
            this.jtbDanhSach.setModel(dtm);
            }
            else
            {
                JOptionPane.showMessageDialog(this,"Xin nhập mã sách!");
            }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        
        TimNangCao tknc = new TimNangCao(this,true);
        tknc.setVisible(true);
        String tenSach = tknc.getTenSach();
        String tacGia = tknc.getTacGia();
        String nhaSanXuat = tknc.getNhaSanXuat();
        String gia = tknc.getGia();
        try
        {
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            //tao ket noi toi csdl
            String conectionString = "jdbc:mysql://localhost:3306/bookonlinev3";
            Properties pros = new Properties();
            pros.setProperty("characterEncoding", "utf8");
            //pros.setProperty("root","root");
            Connection connect = (Connection) DriverManager.getConnection(conectionString, "root", "root");
             //tao statement
            Statement sm = connect.createStatement();
            Vector vt = (Vector)  this.jcbTheLoai.getSelectedItem();
            int MaLoaiSach = (Integer) vt.get(0);            
                       
            if(!tenSach.equals("")||!tacGia.equals("") ||!nhaSanXuat.equals("")||!gia.equals("")){
            String sql = "SELECT *" + "FROM sach"
                        + " WHERE ten_sach LIKE '%" + tenSach 
                        + "%' AND tac_gia LIKE '%" + tacGia 
                        + "%' AND gia_ban LIKE '%" + gia +"%'";   
//                        + "%' IN (SELECT * FROM (SELECT * FROM sach INNER JOIN nha_xuat_ban ON sach.ma_nha_xuat_ban = nha_xuat_ban.ma_nha_xuat_ban) "
//                        + "WHERE ten_nha_xuat_Ban LIKE '%"+nhaSanXuat+"%')";        
            ResultSet rs = sm.executeQuery(sql);
            
            //Tao cac cot tren tabel
            DefaultTableModel dtm = new DefaultTableModel();
            
            dtm.addColumn("Mã Sách");
            dtm.addColumn("Tên Sách");
            dtm.addColumn("Tác Giả");
            dtm.addColumn("Số Lượng Tồn");
            dtm.addColumn("Ngày Nhập");
            dtm.addColumn("Giá Bán");
            
            //doc du lieu
             while(rs.next())
            { 
                Vector ct = new Vector();
                ct.add(rs.getString("ma_sach"));
                ct.add(rs.getString("ten_sach"));
                ct.add(rs.getString("tac_gia"));
                ct.add(rs.getString("so_luong_ton"));
                ct.add(rs.getString("ngay_nhap"));
                ct.add(rs.getString("gia_ban"));
                dtm.addRow(ct);                
            }
            
            //gan model cho jtable
            this.jtbDanhSach.setModel(dtm);
            }
            else
            {
                JOptionPane.showMessageDialog(this,"Xin nhập thông tin tìm kiếm!");
            }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    
    
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        
        try{
            DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
            //tao ket noi toi csdl
            String conectionString = "jdbc:mysql://localhost:3306/bookonlinev3";
            Properties pros = new Properties();
            pros.setProperty("characterEncoding", "utf8");
            //pros.setProperty("root","root");
            Connection connect = (Connection) DriverManager.getConnection(conectionString, "root", "root");
            
            //tao statement
            Statement sm = connect.createStatement();            
            String sql = "SELECT *" + "FROM loai_sach";            
            ResultSet rs = sm.executeQuery(sql);          
      
            
            //Đọc sữ liệu
            DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
            while(rs.next())
            {
                Vector ct = new Vector();
                ct.add(rs.getInt("ma_loai_sach"));
                ct.add(rs.getString("ten_loai_sach"));
                dcbm.addElement(ct);
            }
            
            //gan model cho jcombobox
            this.jcbTheLoai.setModel(dcbm);
            //dong ket noi
            connect.close();
            
            
        
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }               
       
    }//GEN-LAST:event_formWindowOpened

    private void jcbTheLoaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTheLoaiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbTheLoaiActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JComboBox jcbTheLoai;
    private javax.swing.JTable jtbDanhSach;
    // End of variables declaration//GEN-END:variables
}
